package com.ctshk.rpc.airexpress.mapper;

import com.ctshk.rpc.airexpress.base.CustomBaseMapper;
import com.ctshk.rpc.airexpress.entity.AirportExpressIncreaseStrategy;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 产品加幅策略表 Mapper 接口
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-02-05
 */
public interface AirportExpressIncreaseStrategyMapper extends CustomBaseMapper<AirportExpressIncreaseStrategy> {
    @Select({"<script>",
            "select id from t_airport_express_increase_strategy where 1=1",
            "and airport_Express_Id in <foreach collection='list' item='item' open='(' close=')' separator=','>",
            "#{item}",
            "</foreach>",
            "</script>"})
    List<Long> selectIdListByAirportExpressIds(List<Long> airportExpressIds);

    @SelectProvider(value = AirportExpressIncreaseStrategyProvider.class, method = "selectByAirportExpressIdNowDate")
    AirportExpressIncreaseStrategy selectByAirportExpressIdNowDate(@Param("airportExpressId") Long airportExpressId, @Param("nowDate") String nowDate);

    /**
     * 根据产品ids和当前日期，查询生效的加幅策略
     * @param airportExpressIds
     * @param nowDate
     * @return
     */
    @Select({"<script>",
            "select aeis.id, aeis.increase_strategy_name, aeis.self_sale_permission_type, aeis.agent_sale_permission_type, aeis.airport_express_id, aeis.self_sale_permission_id, aeis.agent_sale_permission_id",
            "from t_airport_express_increase_strategy aeis LEFT JOIN t_airport_express_increase_strategy_date aeisd ON (aeis.id = aeisd.increase_strategy_id)",
            "where #{nowDate} between aeisd.date_begin AND aeisd.date_end",
            "and airport_Express_Id in <foreach collection='airportExpressIds' item='item' open='(' close=')' separator=','>",
            "#{item}",
            "</foreach>",
            "</script>"})
    List<AirportExpressIncreaseStrategy> selectByAirportExpressIdsNowDate(@Param("airportExpressIds") List<Long> airportExpressIds, @Param("nowDate") String nowDate);

    @Select({"<script>",
            "SELECT IFNULL(" +
            "(select aeis.agent_sale_permission_type " +
            "from t_airport_express_increase_strategy_date aeisd " +
            "LEFT JOIN (select taeis.id, taeis.agent_sale_permission_type from t_airport_express_increase_strategy taeis where taeis.airport_express_id = #{id}) aeis " +
            "ON aeisd.increase_strategy_id = aeis.id " +
            "where #{date} BETWEEN aeisd.date_begin and aeisd.date_end" +
            " and aeis.agent_sale_permission_type is not null limit 1)" +
            ", (SELECT 2 FROM t_airport_express_initialization_settings aeis," +
            "t_airport_express_initialization_settings_channel aeisc WHERE " +
            "aeis.id = aeisc.setting_id " +
            "AND aeisc.channel_type = 1 " +
            "AND aeisc.channel_id = 2 )) from dual",
            "</script>"})
    Integer selectAgentSaleTypeByIdDate(@Param("id") Long id, @Param("date")LocalDate now);
}
